package com.lyw;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo {
    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
        Class<Student> clz = Student.class;

        Student student = clz.newInstance();

        Method setName = clz.getDeclaredMethod("setName", String.class);
        Method setId =clz.getDeclaredMethod("setId", int.class);
        setId.invoke(student,1);
        setName.invoke(student,"政黄");

        Method sayHello = clz.getDeclaredMethod("sayHello");

        sayHello.invoke(student);

        Constructor<Student> declaredConstructor = clz.getDeclaredConstructor(String.class);

        declaredConstructor.setAccessible(true);

        Student student1 =declaredConstructor.newInstance("政黄111");

        sayHello.invoke(student1);

        Field name = clz.getDeclaredField("name");
        name.setAccessible(true);
        name.set(student1,"政黄222");
        sayHello.invoke(student1);
        Field id = clz.getDeclaredField("id");
        id.setAccessible(true);
        id.setInt(student1,2);
        sayHello.invoke(student1);
    }
}
